#include "point_add_dialog.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVariant>
#include "database_manager.h"

PointAddDialog::PointAddDialog(PointInfo::Type type, int64_t station_id, QWidget *parent)
    : QDialog(parent),
      type_(type),
      station_id_(station_id),
      point_number_spin_(new QSpinBox(this)),
      physical_info_edit_(new QLineEdit(this)),
      ok_button_(new QPushButton("确定", this)),
      cancel_button_(new QPushButton("取消", this)),
      related_point_combo_(nullptr)
{
    setupUI();
    loadRelatedPoints();
}

PointAddDialog::~PointAddDialog()
{
}

void PointAddDialog::setupUI()
{
    setWindowTitle("新增点表");
    setMinimumWidth(300);

    QVBoxLayout *main_layout = new QVBoxLayout(this);

    // 点号
    QHBoxLayout *point_layout = new QHBoxLayout();
    QLabel *point_label = new QLabel("点号：", this);
    point_number_spin_->setRange(0, 65536);
    point_layout->addWidget(point_label);
    point_layout->addWidget(point_number_spin_);
    main_layout->addLayout(point_layout);

    // 物理信息
    QHBoxLayout *info_layout = new QHBoxLayout();
    QLabel *info_label = new QLabel("物理信息：", this);
    info_layout->addWidget(info_label);
    info_layout->addWidget(physical_info_edit_);
    main_layout->addLayout(info_layout);

    // 关联遥信（仅遥控和遥调需要）
    if (type_ == PointInfo::Type::YK || type_ == PointInfo::Type::YT)
    {
        related_point_combo_ = new QComboBox(this);
        QHBoxLayout *related_layout = new QHBoxLayout();
        QLabel *related_label = new QLabel("关联信号：", this);
        related_layout->addWidget(related_label);
        related_layout->addWidget(related_point_combo_);
        main_layout->addLayout(related_layout);
    }

    // 按钮
    QHBoxLayout *button_layout = new QHBoxLayout();
    button_layout->addStretch();
    button_layout->addWidget(ok_button_);
    button_layout->addWidget(cancel_button_);
    main_layout->addLayout(button_layout);

    connect(ok_button_, &QPushButton::clicked, this, &QDialog::accept);
    connect(cancel_button_, &QPushButton::clicked, this, &QDialog::reject);
}

void PointAddDialog::loadRelatedPoints()
{
    if (type_ != PointInfo::Type::YK && type_ != PointInfo::Type::YT || !related_point_combo_)
    {
        return;
    }

    // 根据类型查询对应的点表
    PointInfo::Type query_type = (type_ == PointInfo::Type::YK) ? 
        PointInfo::Type::YX : PointInfo::Type::YC;
    
    QVector<PointInfo> points = DatabaseManager::queryPoints(station_id_, query_type);
    
    // 清空并添加点表数据
    related_point_combo_->clear();
    for (const auto& point : points)
    {
        related_point_combo_->addItem(QString("%1").arg(point.point_desc),
                                    point.point_id);
    }
}

int PointAddDialog::getPointNumber() const
{
    return point_number_spin_->value();
}

QString PointAddDialog::getPhysicalInfo() const
{
    return physical_info_edit_->text();
}

int PointAddDialog::getRelatedPoint() const
{
    if ((type_ == PointInfo::Type::YK || type_ == PointInfo::Type::YT) && related_point_combo_)
    {
        return related_point_combo_->currentData().toInt();
    }
    return -1;
} 